package cn.net.shuxing.yifu.common.front.controller;

import cn.net.shuxing.yifu.common.bean.User;
import cn.net.shuxing.yifu.common.bean.UserCapital;
import cn.net.shuxing.yifu.common.bean.UserDealHistory;
import cn.net.shuxing.yifu.common.bean.UserTransfer;
import cn.net.shuxing.yifu.common.constant.DealScene;
import cn.net.shuxing.yifu.common.constant.DealType;
import cn.net.shuxing.yifu.common.constant.UserType;
import cn.net.shuxing.yifu.common.dto.BaseDto;
import cn.net.shuxing.yifu.common.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

@RestController
public class NoviceController extends BaseController {

    @Autowired
    private UserService userService;

    @Autowired
    private UserDealHistoryService userDealHistoryService;

    @Autowired
    private UserRoleService userRoleService;

    @Autowired
    private UserCapitalService userCapitalService;

    @RequestMapping("/front/my/novice/state")
    public BaseDto getNoviceState(Integer userId) {
        BaseDto baseDto = checkUserId(userId);
        if (baseDto != null && baseDto.isError())
            return baseDto;

        List<Integer> integers = userRoleService.getsByUserId(userId);
        boolean unActive = false;
        boolean member = false;
        if (integers != null && !integers.isEmpty()) {
            for (Integer integer : integers) {
                if (integer == 0) {
                    unActive = true;
                }
                if (integer == 1) {
                    member = true;
                }
            }
        }
        if (unActive) {
            return BaseDto.ok(2);
        }
        if (member) {
            List<UserDealHistory> userTransfers = userDealHistoryService.getsMemberTransferHistoryByUserId(userId);
            if (userTransfers == null || userTransfers.isEmpty()) {
                return BaseDto.ok(3);
            } else {
                List<UserDealHistory> hangBuyList = userDealHistoryService.getsByDealTypeAndUserId(DealType.HANG_BUY, userId);
                List<UserDealHistory> hangSaleList = userDealHistoryService.getsByDealTypeAndUserId(DealType.HANG_SALE, userId);
                if (hangBuyList != null && !hangBuyList.isEmpty())
                    return BaseDto.ok(5);
                if (hangSaleList != null && !hangSaleList.isEmpty())
                    return BaseDto.ok(5);
                if (hangBuyList == null || hangBuyList.isEmpty())
                    return BaseDto.ok(4);
                if (hangSaleList == null || !hangSaleList.isEmpty())
                    return BaseDto.ok(4);
            }


//            List<UserCapital> userCapitals = userCapitalService.getsByUserIdAndUserType(userId, UserType.MEMBER);
//            if (userCapitals != null && !userCapitals.isEmpty()) {
//                BigDecimal dealCount = new BigDecimal(0);
//                for (UserCapital userCapital : userCapitals) {
//                    dealCount = dealCount.add(userCapital.getTotalDealCount());
//                }
//                if (dealCount.compareTo(new BigDecimal(0)) > 0) {
//                    return BaseDto.ok(5);
//                }
//            }


        } else
            return BaseDto.error("不需要调用此接口");
        return BaseDto.ok(5);
    }
}
